package com.warren.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.enums.RoleStatusEnum;
import com.warren.response.Result;
import com.warren.system.entity.Menu;
import com.warren.system.entity.Role;
import com.warren.system.service.MenuService;
import com.warren.system.service.RoleService;
import com.warren.system.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-03-21
 */
@Api(value = "角色管理",tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "分页查询角色")
    @PostMapping("/findRolePage")
    public Result findUserPage(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "7") Integer size,
                               @RequestBody RoleVO roleVO){
        //取得分页条件
        Page<Role> page = new Page<>(current,size);
        //获得wrapper
        QueryWrapper<Role> wrapper = getWrapper(roleVO);
        //执行查询
        IPage<Role> userPage = roleService.page(page,wrapper);

        long total = userPage.getTotal();
        List<Role> records = userPage.getRecords();

        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "导出角色excel失败",operation = "导出角色excel")
    @ApiOperation(value = "导出excel",notes = "导出所有角色的excel表格")
    @PostMapping("/excel")
    public void export(HttpServletResponse response)throws IOException{
        List<Role> roles = roleService.list();
        roleService.exportRoleExcel(response,roles);
    }

    @ApiOperation(value = "角色拥有的菜单和所有菜单")
    @GetMapping("/findRoleMenu")
    public Result findRoleMenu(Long id){
        List<Menu> tree = menuService.permsTreeBuild(menuService.list());
        //角色拥有的菜单id
        List<Long> mids = roleService.findMenuIdsByRoleId(id);
        Map<String,Object> map = new HashMap<>();
        map.put("tree",tree);
        map.put("mids",mids);
        return Result.ok().data(map);
    }

    @ControllerEndpoint(exceptionMessage = "角色授权失败",operation = "角色授权失败")
    @ApiOperation(value = "角色授权")
    @PostMapping("/authority")
    public Result authority(Long id,@RequestBody Long[] mids){
        roleService.authority(id,mids);
        return Result.ok();
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/getRoleList")
    public Result getRoleList(){
        List<Role> roles = roleService.list();
        return Result.ok().data("records",roles);
    }

    @ControllerEndpoint(exceptionMessage = "添加角色失败",operation = "添加角色")
    @ApiOperation(value = "添加角色")
    @PostMapping("/add")
    public Result add(@RequestBody RoleVO roleVO){
        Role role = new Role();
        role.setRoleName(roleVO.getRoleName());
        role.setRemark(roleVO.getRemark());
        role.setStatus(RoleStatusEnum.AVAILABLE.getStatusCode());
        roleService.save(role);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除角色失败",operation = "删除角色")
    @ApiOperation(value = "删除角色",notes = "根据id删除角色信息")
    @DeleteMapping("/delete")
    public Result delete(Long id){
        return roleService.delRole(id);
    }

    @ControllerEndpoint(exceptionMessage = "更新角色失败",operation = "更新角色")
    @ApiOperation(value = "更新角色",notes = "根据id更新角色信息")
    @PutMapping("/update")
    public Result update(Long id,@RequestBody RoleVO roleVO){
        Role role = new Role();
        role.setId(id);
        role.setRoleName(roleVO.getRoleName());
        role.setRemark(roleVO.getRemark());
        roleService.updateById(role);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "角色更新状态",operation = "角色|禁用/启动")
    @ApiOperation(value = "更新状态",notes = "禁用和更新两种状态")
    @PutMapping("/updateStatus")
    public Result updateStatus(Long id, Integer status){
        Role role = new Role();
        role.setId(id);
        role.setStatus(status);
        roleService.updateStatus(role);
        return Result.ok();
    }

    private QueryWrapper<Role> getWrapper(RoleVO roleVO) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (roleVO != null){
            //查询每个条件
            if (!ObjectUtils.isEmpty(roleVO.getRoleName())){
                queryWrapper.like("role_name",roleVO.getRoleName());
            }
        }
        return queryWrapper;
    }
}

